1684F - Diverse Segments - CodeForces Solution


data structures two pointers *2600

Please click on ads to support us..

C++ Code:

#include<iostream>
#include <map>
#include <vector>
using namespace std;
int t,n,m;
map<int,vector<int>> h;
int main(){
	cin>>t;
	while(t--){
	cin>>n>>m;
	vector<int> a(n+1),l(n+1,n+1),mxl(n+1,n+1);
	int x,y,ans=n,st=n+1,en=0,minL=n+1;
	h.clear();
	for(int i=1;i<=n;i++){
		cin>>a[i];
		h[a[i]].push_back(i);
	}
	for(int i=0;i<m;i++){
		cin>>x>>y;
		l[y]=min(l[y],x);
	}
	for(int i=n;i>0;i--){
		minL=min(minL, l[i]);
		vector<int> *tmp=&h[a[i]];
		tmp->pop_back();
		int len=tmp->size();
		if (minL<i){
			int la=len?tmp->at(len-1):-1;
			if (minL<=la){
				en=max(la, en);
				vector<int>::iterator it=lower_bound(tmp->begin(),tmp->end(),minL);
				mxl[i-1]=*it;
				++it;
				st=min(st,(*it?*it:i));
			}
		}
	}
	for(int i=n;i>=en;i--){
		st=min(st, mxl[i]);
		ans=min(ans,max(0,i-st+1));
	}
	cout<<ans<<endl;	}
}


Comments

Submit
0 Comments
More Questions

3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches